package mi.payment.transaction.authorize.base;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.DatePicker;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import lainey.ui.dialog.DateDialog;
import lainey.ui.dialog.EditTextDialog;
import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.transaction.AutoFinishActivity;
import mi.payment.ui.other.OnlyInputAmountActivity;
import mi.payment.utils.MoneyUtils;
import sunmi.payment.L3.L3BroadcastHelper;
import sunmi.payment.L3.L3ErrorCode;

/**
 * 输入信息界面
 *
 * @author longx on 2017/3/7.
 */
public class PreAuthorizeInputInfoActivity extends AutoFinishActivity {

    public TextView mTvTitle;
    public TextView mTvDate;
    public TextView mTvAmount;
    public TextView mTvOrderNo;
    public View mAmountView;

    public long mAmount;

    private DateDialog mDateDialog;
    private EditTextDialog mEditTextDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.authorize_activity_input_original_info);
        initView();
    }

    private void initView() {
        TitleView titleView = findViewById(R.id.title_view);
        mTvTitle = titleView.getCenterTextView();
        titleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );

        View item = findViewById(R.id.item_auth_num);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        TextView leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.transaction_original_auth_number);
        mTvOrderNo = item.findViewById(R.id.right_text);
        mTvOrderNo.setText(R.string.unfilled);
        item.setOnClickListener(this);

        item = findViewById(R.id.item_date);
        item.findViewById(R.id.left_image).setVisibility(View.GONE);
        leftText = item.findViewById(R.id.left_text);
        leftText.setText(R.string.transaction_original_date);
        mTvDate = item.findViewById(R.id.right_text);
        mTvDate.setText(R.string.unfilled);
        item.setOnClickListener(this);

        mAmountView = findViewById(R.id.item_amount);
        mAmountView.findViewById(R.id.left_image).setVisibility(View.GONE);
        leftText = mAmountView.findViewById(R.id.left_text);
        leftText.setText(R.string.money);
        mTvAmount = mAmountView.findViewById(R.id.right_text);
        mTvAmount.setText(R.string.unfilled);
        mAmountView.setOnClickListener(this);

        findViewById(R.id.mb_ok).setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        L3BroadcastHelper.sendFailedBroadcast(mContext, L3ErrorCode.ERROR_TRADE_CANCEL);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismissDateDialog();
        dismissEditTextDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null && resultCode == OnlyInputAmountActivity.RESULT_CODE) {
            String val = data.getStringExtra(OnlyInputAmountActivity.RESULT_AMOUNT);
            if (val != null && val.trim().length() > 0) {
                mAmount = MoneyUtils.stringMoney2LongCent(val);
                String str = getString(R.string.money_only_symbol) + val;
                mTvAmount.setText(str);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.item_auth_num:
                showEditTextDialog();
                break;
            case R.id.item_date:
                showDateDialog();
                break;
            case R.id.item_amount:
                Intent intent = new Intent(this, OnlyInputAmountActivity.class);
                startActivityForResult(intent, OnlyInputAmountActivity.RESULT_CODE);
                break;
        }
    }

    private void showEditTextDialog() {
        if (mEditTextDialog == null) {
            mEditTextDialog = new EditTextDialog(this);
            mEditTextDialog.setRightText(R.string.ui_ok);
            mEditTextDialog.setLeftText(R.string.ui_cancel);
            mEditTextDialog.setTitleText(R.string.transaction_please_input_original_auth_number);
            mEditTextDialog.setCanceledOnTouchOutside(true);
            mEditTextDialog.setCancelable(true);
            mEditTextDialog.setTextMode();
            mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
            mEditTextDialog.setLeftOnClickListener(
                    v -> dismissEditTextDialog()
            );
            mEditTextDialog.setRightOnClickListener(
                    v -> {
                        String text = mEditTextDialog.getText();
                        if (text.trim().length() > 0) {
                            try {
                                long parseLong = Long.parseLong(text);
                                String val = String.format("%06d", parseLong);
                                mTvOrderNo.setText(val);
                                dismissEditTextDialog();
                            } catch (Exception e) {
                                e.printStackTrace();
                                showToast(R.string.error_input_auth_number_hint);
                            }
                        } else {
                            showToast(R.string.error_input_auth_number_hint);
                        }
                    }
            );
        }
        String orderId = mTvOrderNo.getText().toString();
        boolean equals = TextUtils.equals(getString(R.string.unfilled), orderId);
        if (equals) {
            mEditTextDialog.setText("");
        } else {
            mEditTextDialog.setText(orderId);
        }
        mEditTextDialog.setMaxLength(6);
        try {
            mEditTextDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showDateDialog() {
        if (mDateDialog == null) {
            mDateDialog = new DateDialog(this);
            mDateDialog.setRightText(R.string.ui_ok);
            mDateDialog.setLeftText(R.string.ui_cancel);
            mDateDialog.setTitleText(R.string.transaction_please_input_original_date);
            mDateDialog.setCancelable(true);
            mDateDialog.setCanceledOnTouchOutside(true);
            mDateDialog.setLeftOnClickListener(
                    v -> dismissDateDialog()
            );
            mDateDialog.setRightOnClickListener(
                    v -> {
                        DatePicker datePicker = mDateDialog.getDatePicker();
                        int year = datePicker.getYear();
                        int month = datePicker.getMonth();
                        int dayOfMonth = datePicker.getDayOfMonth();
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(year, month, dayOfMonth);
                        // 将当前时间进行格式化
                        Date time = calendar.getTime();
                        String date = new SimpleDateFormat("MM/dd").format(time);
                        mTvDate.setText(date);
                        dismissDateDialog();
                    }
            );
            int color = getResources().getColor(R.color.colorPrimary);
            mDateDialog.setDatePickerDividerColor(color);
        }
        try {
            mDateDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void dismissDateDialog() {
        if (mDateDialog != null) {
            mDateDialog.dismiss();
        }
    }

    private void dismissEditTextDialog() {
        if (mEditTextDialog != null) {
            mEditTextDialog.dismiss();
        }
    }


}
